﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TireService.Data;

namespace TireService.Website
{
    public partial class GodkendTilbud : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack != true)
            {
                if (Request.QueryString["id"] == null)
                {
                    Response.Redirect("~/Index.aspx");
                }
                else
                {
                    string id = Request.QueryString["id"].ToString();
                    int action = Convert.ToInt32(Request.QueryString["action"]);

                    TireServiceDB db = new TireServiceDB();

                    CustomerTireSetQuotationList quotation = db.CustomerTireSetQuotationLists.SingleOrDefault(p => p.QuotationReferenceId == new Guid(id));

                    if (quotation == null)
                    {
                        Response.Redirect("~/Index.aspx");
                    }

                    if (quotation.Approved != null & action != 2)
                    {
                        pQuotationAlreadyApproved.Visible = true;
                    }
                    else
	                {
                        if (action == 0)
                        {
                            // Quotation rejected
                            pRejected.Visible = true;
                        }
                        if (action == 1)
                        {
                            // Quotation accepted
                            pAccept.Visible = true;
                        }
                        if (action == 2)
                        {
                            // Quotation ordered
                            pCompleted.Visible = true;
                        }
                        if (action == 3)
                        {
                            // Quotation reject confirmation
                            pRejectedCompleted.Visible = true;
                        }
                        if (action == 4)
                        {
                            // Quotation rejection failed
                            pRejectedFail.Visible = true;
                        }
	                }
                }
            }
        }

        protected void btnAccept_Click(object sender, EventArgs e)
        {
            //string id = Request.QueryString["id"].ToString();
            //int action = 2;

            //TireServiceDB db = new TireServiceDB();

            //TireQuotationView quotation = db.TireQuotationViews.SingleOrDefault(p => p.QuotationReferenceId == new Guid(id));

            //string Qty = quotation.Quantity.ToString();
            //decimal tirepriceexvat = (Convert.ToDecimal(quotation.CostPrice) + CalculateTirePrice(Convert.ToInt32(quotation.TireSize)));
            //decimal envpriceexvat = 10;
            //decimal montpriceexvat = CalculateMountingPrice(Convert.ToInt32(quotation.TireSize));
            //decimal totalexvat = (tirepriceexvat * quotation.Quantity) + (envpriceexvat * quotation.Quantity) + (montpriceexvat * quotation.Quantity);
            //decimal vat = totalexvat * Convert.ToDecimal(0.25);
            //decimal total = totalexvat * Convert.ToDecimal(1.25);
            //string TirePriceExVat = string.Format("{0:N}", tirepriceexvat);
            //string TirePriceTotalExVat = string.Format("{0:N}", (tirepriceexvat * quotation.Quantity));
            //string EnvPriceTotalExVat = string.Format("{0:N}", (envpriceexvat * quotation.Quantity));
            //string MontPriceExVat = string.Format("{0:N}", montpriceexvat);
            //string MontPriceTotalExVat = string.Format("{0:N}", (montpriceexvat * quotation.Quantity));
            //string TotalExVat = string.Format("{0:N}", totalexvat);
            //string TotalVat = string.Format("{0:N}", vat);
            //string Total = string.Format("{0:N}", total);
            
            //// Send email to customer
            //EmailNotificationServices emailBLL = new EmailNotificationServices();
            //emailBLL.SendTireQuotationAcceptance(quotation.Email, quotation.CustomerId, quotation.Name, quotation.RegistrationNumber, quotation.Description, Qty, TirePriceExVat, TirePriceTotalExVat, EnvPriceTotalExVat, MontPriceExVat, MontPriceTotalExVat, TotalExVat, TotalVat, Total);

            //// update quotation
            //CustomerTireSetQuotationList updatequotation = db.CustomerTireSetQuotationLists.SingleOrDefault(p => p.QuotationReferenceId == new Guid(id));
            //updatequotation.Approved = DateTime.Now;
            //updatequotation.Rejected = null;
            //db.SubmitChanges();

            //Response.Redirect("~/GodkendTilbud.aspx?action=" + action.ToString() + "&id=" + quotation.QuotationReferenceId.ToString());
        }

        protected void btnReject_Click(object sender, EventArgs e)
        {
            //string id = Request.QueryString["id"].ToString();
            //int action = 3;

            //TireServiceDB db = new TireServiceDB();

            //TireQuotationView quotation = db.TireQuotationViews.SingleOrDefault(p => p.QuotationReferenceId == new Guid(id));

            //if (quotation.Approved != null)
            //{
            //    action = 4;
            //    Response.Redirect("~/GodkendTilbud.aspx?action=" + action.ToString() + "&id=" + quotation.QuotationReferenceId.ToString());
            //}
            //else
            //{
            //    string Qty = quotation.Quantity.ToString();
            //    decimal tirepriceexvat = (Convert.ToDecimal(quotation.CostPrice) + CalculateTirePrice(Convert.ToInt32(quotation.TireSize)));
            //    decimal envpriceexvat = 10;
            //    decimal montpriceexvat = CalculateMountingPrice(Convert.ToInt32(quotation.TireSize));
            //    decimal totalexvat = (tirepriceexvat * quotation.Quantity) + (envpriceexvat * quotation.Quantity) + (montpriceexvat * quotation.Quantity);
            //    decimal vat = totalexvat * Convert.ToDecimal(0.25);
            //    decimal total = totalexvat * Convert.ToDecimal(1.25);
            //    string TirePriceExVat = string.Format("{0:N}", tirepriceexvat);
            //    string TirePriceTotalExVat = string.Format("{0:N}", (tirepriceexvat * quotation.Quantity));
            //    string EnvPriceTotalExVat = string.Format("{0:N}", (envpriceexvat * quotation.Quantity));
            //    string MontPriceExVat = string.Format("{0:N}", montpriceexvat);
            //    string MontPriceTotalExVat = string.Format("{0:N}", (montpriceexvat * quotation.Quantity));
            //    string TotalExVat = string.Format("{0:N}", totalexvat);
            //    string TotalVat = string.Format("{0:N}", vat);
            //    string Total = string.Format("{0:N}", total);
            
            //    // Send email to customer
            //    EmailNotificationServices emailBLL = new EmailNotificationServices();
            //    emailBLL.SendTireQuotationRejection(quotation.Email, quotation.CustomerId, quotation.Name, quotation.RegistrationNumber, quotation.Description, Qty, TirePriceExVat, TirePriceTotalExVat, EnvPriceTotalExVat, MontPriceExVat, MontPriceTotalExVat, TotalExVat, TotalVat, Total);

            //    // update quotation
            //    CustomerTireSetQuotationList updatequotation = db.CustomerTireSetQuotationLists.SingleOrDefault(p => p.QuotationReferenceId == new Guid(id));
            //    updatequotation.Rejected = DateTime.Now;
            //    db.SubmitChanges();

            //    Response.Redirect("~/GodkendTilbud.aspx?action=" + action.ToString() + "&id=" + quotation.QuotationReferenceId.ToString());
            //}
        }

        private decimal CalculateTirePrice(int TireSize)
        {
            decimal price = 125;

            if (TireSize < 16)
            {
                price = 100;
            }

            if (TireSize > 15 & TireSize < 18)
            {
                price = 125;
            }

            if (TireSize > 17)
            {
                price = 150;
            }

            return price;
        }

        private decimal CalculateMountingPrice(int TireSize)
        {
            decimal price = 135;

            if (TireSize > 17)
            {
                price = 180;
            }

            return price;
        }

    }
}